//
//  UIView+Additional.m
//  RaiyiCommon
//
//  Created by zhiwei ma on 12-4-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIView+Additional.h"

@implementation UIView (Additional)

- (BOOL)findAndResignFirstResponder
{
    if (self.isFirstResponder)
    {
        [self resignFirstResponder];
        return YES;
    }
    for (UIView *subView in self.subviews)
    {
        if ([subView findAndResignFirstResponder])
            return YES;
    }
    return NO;
}

- (void)fullRecurrentForSubViews:(void(^)(UIView* view))block
{
    block(self);
    for (UIView* sub in self.subviews)
    {
        [sub fullRecurrentForSubViews:block];
    }
}
@end



#define RYCUIViewWaitingViewTag 9999
@implementation UIView (Waiting)

- (void)startWaiting:(CGRect)frame
{
    if ([self isWaiting])
    {
        return;
    }
    
    UIActivityIndicatorView* waitingView = nil;
    UIView* view = [self viewWithTag:RYCUIViewWaitingViewTag];
    if (nil == view)
    {
        waitingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        waitingView.frame = frame;
        waitingView.hidesWhenStopped = YES;
        waitingView.tag = RYCUIViewWaitingViewTag;
        [self addSubview:waitingView];
        [waitingView release];
    }
    
    [waitingView startAnimating];
}

- (void)startWaiting
{
    if ([self isWaiting])
    {
        return;
    }
    
    UIActivityIndicatorView* waitingView = nil;
    UIView* view = [self viewWithTag:RYCUIViewWaitingViewTag];
    if (nil == view)
    {
        if ([UIActivityIndicatorView instancesRespondToSelector:@selector(setColor:)]) //ios5
        {
            waitingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            waitingView.color = [UIColor grayColor];
        }
        else //earlier
        {
            waitingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            waitingView.frame = CGRectMake(0,0,30,30);
        }
        waitingView.hidesWhenStopped = YES;
        waitingView.tag = RYCUIViewWaitingViewTag;
        waitingView.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
        waitingView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        [self addSubview:waitingView];
        [waitingView release];
    }
    
    [waitingView startAnimating];
}

- (void)stopWaiting
{
    UIActivityIndicatorView*  waitingView = (UIActivityIndicatorView*)[self viewWithTag:RYCUIViewWaitingViewTag];
    if (waitingView)
    {
        [waitingView stopAnimating];
    }
    [waitingView removeFromSuperview];
}

- (BOOL)isWaiting
{
    UIActivityIndicatorView* activity = (UIActivityIndicatorView*)[self viewWithTag:RYCUIViewWaitingViewTag];
    if (activity)
    {
        return YES;
    }
    return NO;
}
@end




@implementation UITableView (Additional)

//- (void)clearBackground
//{
//    if (UITableViewStyleGrouped == self.style)
//    {
//        CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue];
//        if (version >= 6.0)
//        {
//            self.backgroundView = nil;
//        }
//        else
//        {
//            self.backgroundColor = [UIColor clearColor];
//        }
//    }
//    else
//    {
//        self.backgroundColor = [UIColor clearColor];
//    }
//}

- (void)setTableViewBackgroundColor:(UIColor*)color
{
    self.backgroundColor = color;
    if (UITableViewStyleGrouped == self.style)
    {
        CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue];
        if (version >= 6.0)
        {
            [[self.backgroundView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
            UIView* bg = [[UIView alloc] initWithFrame:self.backgroundView.bounds];
            bg.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
            bg.backgroundColor = color;
            [self.backgroundView addSubview:bg];
            [bg release];
        }
    }
}
@end
